package com.aoratec.demo.network;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;


public class DemoJsonHttphandler {
	
	public static final String KEY_TAG = "TAG";
	public static final String KEY_STATUS = "STATUS";
	public static final String KEY_ERRORMESSAGE = "ERRORMESSAGE";
	
	public static final String KEY_QBEFORE = "QBEFORE";
	public static final String KEY_QUESTNUM = "QUESTNUM";
	
	
	public static final int QUESTNUM = 20;
	
//	public static final String KEY_
	
	public static final String TAG_ACTIVITY = "REWARD_LIST";
	public static final String TAG_CLOSED = "REWARD_FINISH";
	

	
	protected  final DemoJsonHttpResponseHandler handler;
	
////	private RequestParams pr;
//	public DemoJsonHttphandler(Map params, boolean bHttps, DemoJsonHttpResponseHandler handler1){
//		this.handler = handler1;
//		
//
//		JSONObject jsono = new JSONObject(params);
//		RequestParams rp = new RequestParams();
//		rp.put("data", jsono.toString());
//		
//		Log.d("Demo", "Request " + rp);
//		AsyncHttpClient client = new AsyncHttpClient();
//		client.setTimeout(10000);
//		client.post("http://test.myaora.net:81/api.php?TAG=GIONEE_RANK_APP", rp, new JsonHttpResponseHandler(){
//			@Override
//			public void onSuccess(int statusCode, JSONObject response) {
//				Log.d("Demo", "onSuccess " + response);
//					if(handler != null){
//						handler.onSuccess(statusCode, response);
//					}
//			}
//
//			@Override
//			public void onFailure(Throwable e, JSONObject errorResponse) {
//				Log.d("Demo", "onFailure " + errorResponse);
//				Log.d("Demo", "onFailure " + e);
//				if(handler != null){
//					handler.onFailure(e, errorResponse);
//				}
//			}
//			
//
//			@Override
//			protected Object parseResponse(String responseBody)
//					throws JSONException {
//				Log.d("Demo", "Recv " + responseBody);
//				return super.parseResponse(responseBody);
//			}
//			
//			
//		});
//	}
	
	public DemoJsonHttphandler(String entity, String contentType, boolean bHttps, DemoJsonHttpResponseHandler handler1){
		this.handler = handler1;
		

		
		StringEntity strEntity = null;
		try {
			strEntity = new StringEntity(entity);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Log.d("Demo", "Request " + entity);
		AsyncHttpClient client = new AsyncHttpClient();
		client.setTimeout(10000);
		client.post(null, "http://adres.myaora.net:81/api.php", strEntity, "application/json", new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
//				Log.d("Demo", "onSuccess " + response);
					if(handler != null){
						handler.onSuccess(statusCode, response);
					}
			}
			
			@Override
			public void onSuccess(int statusCode, JSONArray response) {
//				Log.d("Demo", "onSuccess " + response);
				if(handler != null){
					handler.onSuccess(statusCode, response);
				}
			}

			@Override
			public void onFailure(Throwable e, JSONObject errorResponse) {
				Log.d("Demo", "onFailure " + errorResponse);
				Log.d("Demo", "onFailure " + e);
				if(handler != null){
					handler.onFailure(e, errorResponse);
				}
			}
			

			@Override
			protected Object parseResponse(String responseBody)
					throws JSONException {
				Log.d("Demo", "Recv " + responseBody);
				return super.parseResponse(responseBody);
			}
			
			
		});
	}
	
	public interface DemoJsonHttpResponseHandler{
		public void onSuccess(int statusCode, JSONObject response);
		public void onSuccess(int statusCode, JSONArray response);
		public void onFailure(Throwable e, JSONObject errorResponse);
	}
}
